﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace FastAccess
{
    public partial class FrmFast : Form
    {
        private bool canClose = false;

        public FrmFast()
        {
            InitializeComponent();
            LoadFastMenu();
        }

        #region FastMenu Methods

        public void LoadFastMenu()
        {
            AddFixedItems();
        }

        private void AddFixedItems()
        {
            if (this.menuFast.Items.Count > 0)
            {
                ToolStripSeparator sep = new ToolStripSeparator();
                this.menuFast.Items.Add(sep);
            }

            this.menuFast.Items.Add(miMainForm);
            this.menuFast.Items.Add(miStartSys);
            this.menuFast.Items.Add(miClose);
        }

        #endregion

        private void ShowSysTrayContextMenu()
        {
            MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
            mi.Invoke(this.sysTray, null);
        }

        private void ReloadSelectedMenu()
        {
            //this.treeMenus.SelectedNode.Name;
        }

        #region Buttons Events

        private void btnNew_Click(object sender, EventArgs e)
        {
            newMenuStrip.Show(btnNew, new System.Drawing.Point(0, btnNew.Height));
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnIcon_Click(object sender, EventArgs e)
        {
            int index = 0;
            string file = null;
            if (Win32.PickIconDlg(ref index, ref file) == 1)
                this.tbIcon.Text = string.Format("{0}, {1}", file, index);
        }

        private void miClose_Click(object sender, EventArgs e)
        {
            this.canClose = true;
            sysTray.Visible = false;
            Application.Exit();
        }

        #endregion

        private void FrmFast_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (canClose)
            {
                //sysTray.Visible = false;
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        private void sysTray_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void sysTray_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this.menuFast.Items.Count > 0)
                {
                    this.sysTray.ContextMenuStrip = this.menuFast;
                    ShowSysTrayContextMenu();
                }
                else
                    this.sysTray.ShowBalloonTip(20, "FastMenu", "No menus added...\nClick here to add.", ToolTipIcon.Info);
            }
            else
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    this.sysTray.ContextMenuStrip = this.contextMenu;
                    ShowSysTrayContextMenu();
                }
        }

    }
}
